#' 
#' #Figures
#' 
#' 
#' ## Executive probabilities 
#' 
#' 
#' ### Hypothesis 1
#' 
#' 
#' #### Predictions
#' 
## ---- echo= TRUE, message = FALSE----------------------------------------------------------------------------------
rm(list=ls())

#install.packages("readxl")
#install.packages("ggplot2")
#install.packages("dplyr")
#install.packages("ggpubr")



library(readxl)
library(ggplot2)
library(dplyr)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 1 executive graph 
########
exec_preds <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp1_executive_ES.xlsx")

exec_preds$Executive[exec_preds$Executive ==  0 ] <- "Loser"
exec_preds$Executive[exec_preds$Executive ==  1 ] <- "Winner"

exec_preds$Gender[exec_preds$Gender ==  0] <- "Male"
exec_preds$Gender[exec_preds$Gender ==  1] <- "Female"

exec_preds$Satisfaction[exec_preds$Satisfaction ==  1] <- "Not At All Satisfied"
exec_preds$Satisfaction[exec_preds$Satisfaction ==  2] <- "Not Very Satisfied"
exec_preds$Satisfaction[exec_preds$Satisfaction ==  3] <- "Fairly Satisfied"
exec_preds$Satisfaction[exec_preds$Satisfaction ==  4] <- "Very Satisfied"

#Reorder the factor levels


exec_preds$Satisfaction <- factor(exec_preds$Satisfaction , levels = c("Not At All Satisfied", "Not Very Satisfied", "Fairly Satisfied", "Very Satisfied"))
exec_preds$Gender <- factor(exec_preds$Gender , levels = c("Male","Female"))
exec_preds$Executive <- factor(exec_preds$Executive , levels = c("Loser","Winner"))


exec_preds$Groups <- paste(exec_preds$Gender, exec_preds$Executive)


#graph this-save as 6.5x6 inches

plot1 <-  ggplot(exec_preds, aes(x = Satisfaction, y = Probabilities, colour = Groups)) +
  labs(x="Satisfaction Category", y="Pr(Fall in Given Category)") +
  ylim(0, .60) +
  theme_bw(16) +  
  theme(legend.position = c(.82, 0.25),  legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank()) +
  geom_point(aes(shape=Groups, color=Groups),size = 3, position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + coord_flip()






#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' #### Differences-Hyp 1
## ------------------------------------------------------------------------------------------------------------------

#rm(list=ls())

library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 1 differences graph 
########
Gaps_probs <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp1_diff_exec.xlsx")


Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  1] <- "Not At All Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  2] <- "Not Very Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  3] <- "Fairly Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  4] <- "Very Satisfied"



Gaps_probs$Satisfaction <- factor(Gaps_probs$Satisfaction , levels = c("Not At All Satisfied", "Not Very Satisfied", "Fairly Satisfied", "Very Satisfied"))



#graph this-save as 6.5x6 inches

plot2 <- ggplot(Gaps_probs, aes(x = Satisfaction, y = Gaps)) +
  labs(x="Satisfaction Category", y="Satisfaction Differences (Boost for Women - Boost for Men)") +  
  ylim(-.03, .03) +
  theme_bw(16) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                         panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + coord_flip() 


#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp1_diff.pdf", width = 8, height = 3.5, units = "in")





#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' #### Put hyp1 executives into one figure
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------

ggarrange(plot1, plot2,
          ncol=1, nrow=2, common.legend = TRUE) #Save as 6.5x6.0 inches

#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp1_exec_fig.pdf", width = 8.5, height = 5.5, units = "in")



#' 
#' 
#' 
#' 
#' 
#' 
#' ### Representation-Executive
#' 
#' 
#' 
#' #### Predictions
#' 
## ------------------------------------------------------------------------------------------------------------------
rm(list=ls())

library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 2 government graph 
########
hyp2_descrip_exec <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_exec_legis.xlsx")

hyp2_descrip_exec$Executive[hyp2_descrip_exec$Executive ==  0 ] <- "Loser"
hyp2_descrip_exec$Executive[hyp2_descrip_exec$Executive ==  1 ] <- "Winner"

hyp2_descrip_exec$Gender[hyp2_descrip_exec$Gender ==  0] <- "Male"
hyp2_descrip_exec$Gender[hyp2_descrip_exec$Gender ==  1] <- "Female"


hyp2_descrip_exec$Gender <- factor(hyp2_descrip_exec$Gender , levels = c("Male","Female"))

hyp2_descrip_exec$Satisfaction[hyp2_descrip_exec$Satisfaction ==  1] <- "Not At All Satisfied"
hyp2_descrip_exec$Satisfaction[hyp2_descrip_exec$Satisfaction ==  2] <- "Not Very Satisfied"
hyp2_descrip_exec$Satisfaction[hyp2_descrip_exec$Satisfaction ==  3] <- "Fairly Satisfied"
hyp2_descrip_exec$Satisfaction[hyp2_descrip_exec$Satisfaction ==  4] <- "Very Satisfied"




hyp2_descrip_exec$Groups <- paste(hyp2_descrip_exec$Gender, hyp2_descrip_exec$Executive)

satisfaction_legis_one <- subset(hyp2_descrip_exec, Satisfaction == "Not At All Satisfied")
satisfaction_legis_two <- subset(hyp2_descrip_exec, Satisfaction == "Not Very Satisfied")
satisfaction_legis_three <- subset(hyp2_descrip_exec, Satisfaction == "Fairly Satisfied")
satisfaction_legis_four <- subset(hyp2_descrip_exec, Satisfaction ==  "Very Satisfied")


#Not at all satisfied
p9 <- ggplot(satisfaction_legis_one, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Not At All Satisfied)") +  
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)



#Not very satisfied 
p10 <- ggplot(satisfaction_legis_two, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Not Very Satisfied)") +  
  ylim(.14, .4) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)





#Fairly satisfied 
p11 <- ggplot(satisfaction_legis_three, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Fairly Satisfied)") +  
  ylim(.34, .6) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)



#Very satisfied 
p12 <- ggplot(satisfaction_legis_four, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Very Satisfied)") +  
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)






#' 
#' 
#' 
#' 
#' #### Differences
#' 
#' 
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------


library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


########
###Hypothesis 2 government graph 
########


Gaps_probs <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_legis_diff_exec.xlsx")

Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  1] <- "Not At All Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  2] <- "Not Very Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  3] <- "Fairly Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  4] <- "Very Satisfied"



Gaps_probs$Gaps_gender <- as.numeric(Gaps_probs$Gaps_gender)
Gaps_probs$Descriptive <- as.numeric(Gaps_probs$Descriptive)
Gaps_probs$Satisfaction <- as.factor(Gaps_probs$Satisfaction)


diff_legis_one <- subset(Gaps_probs, Satisfaction == "Not At All Satisfied")
diff_legis_two <- subset(Gaps_probs, Satisfaction == "Not Very Satisfied")
diff_legis_three <- subset(Gaps_probs, Satisfaction == "Fairly Satisfied")
diff_legis_four <- subset(Gaps_probs, Satisfaction == "Very Satisfied")







#graph this-save as 6.5x6 inches

Legis_diff_1 <- ggplot(diff_legis_one, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)


#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp2_legis_diff.pdf", width = 6.5, height = 6, units = "in")



Legis_diff_2  <- ggplot(diff_legis_two, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)




Legis_diff_3  <- ggplot(diff_legis_three, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)




Legis_diff_4 <- ggplot(diff_legis_four, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)



#' 
#' 
#' 
#' #### Put descriptive into one figure
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------

ggarrange( p9, p10, p11, p12,
           Legis_diff_1, Legis_diff_2, Legis_diff_3, Legis_diff_4,
           ncol=4, nrow=2, common.legend = TRUE) #Save as 6.5x6.0 inches

#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp2_legis_exec.pdf", width = 8.5, height = 5, units = "in")



#' 
#' 
#' ### Representation-Government
#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' ### Gender of Executive-Exec
#' 
## ------------------------------------------------------------------------------------------------------------------
rm(list=ls())

library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 2 government graph 
########
hyp2_hog_exec <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_hog_exec.xlsx")

hyp2_hog_exec$Executive[hyp2_hog_exec$Executive ==  0 ] <- "Loser"
hyp2_hog_exec$Executive[hyp2_hog_exec$Executive ==  1 ] <- "Winner"

hyp2_hog_exec$Gender[hyp2_hog_exec$Gender ==  0] <- "Male"
hyp2_hog_exec$Gender[hyp2_hog_exec$Gender ==  1] <- "Female"

hyp2_hog_exec$HOG[hyp2_hog_exec$HOG ==  0] <- "Male Executive"
hyp2_hog_exec$HOG[hyp2_hog_exec$HOG ==  1] <- "Female Executive"



hyp2_hog_exec$HOG <- as.factor(hyp2_hog_exec$HOG)
hyp2_hog_exec$HOG <- factor(hyp2_hog_exec$HOG , levels = c("Male Executive","Female Executive"))

hyp2_hog_exec$Gender <- factor(hyp2_hog_exec$Gender , levels = c("Male","Female"))


hyp2_hog_exec$Groups <- paste(hyp2_hog_exec$Gender, hyp2_hog_exec$Executive)


hyp2_hog_exec$Satisfaction[hyp2_hog_exec$Satisfaction ==  1] <- "Not At All Satisfied"
hyp2_hog_exec$Satisfaction[hyp2_hog_exec$Satisfaction ==  2] <- "Not Very Satisfied"
hyp2_hog_exec$Satisfaction[hyp2_hog_exec$Satisfaction ==  3] <- "Fairly Satisfied"
hyp2_hog_exec$Satisfaction[hyp2_hog_exec$Satisfaction ==  4] <- "Very Satisfied"


satisfaction_hog_one <- subset(hyp2_hog_exec, Satisfaction == "Not At All Satisfied")
satisfaction_hog_two <- subset(hyp2_hog_exec, Satisfaction == "Not Very Satisfied")
satisfaction_hog_three <- subset(hyp2_hog_exec, Satisfaction == "Fairly Satisfied")
satisfaction_hog_four <- subset(hyp2_hog_exec, Satisfaction == "Very Satisfied")




#Not at all satisfied
p9 <- ggplot(satisfaction_hog_one, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Not At All Satisfied)") +
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +  facet_wrap(. ~ Satisfaction)





#Not very satisfied 
p10 <- ggplot(satisfaction_hog_two, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Not Very Satisfied)") +  
  ylim(.14, .4) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) +  facet_wrap(. ~ Satisfaction)







#Fairly satisfied 
p11 <- ggplot(satisfaction_hog_three, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Fairly Satisfied)") +  
  ylim(.34, .6) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) +  facet_wrap(. ~ Satisfaction)





#Very satisfied 
p12 <- ggplot(satisfaction_hog_four, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Very Satisfied)") +  
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) +  facet_wrap(. ~ Satisfaction)








#' 
#' 
#' ### Differences-HOG
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------



library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 2 government graph 
########
Gaps_probs <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_hog_diff_exec.xlsx")


Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  1] <- "Not At All Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  2] <- "Not Very Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  3] <- "Fairly Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  4] <- "Very Satisfied"


Gaps_probs$HOG[Gaps_probs$HOG ==  0] <- "Male Executive"
Gaps_probs$HOG[Gaps_probs$HOG ==  1] <- "Female Executive"





Gaps_probs$Satisfaction <- factor(Gaps_probs$Satisfaction , levels = c("Not At All Satisfied", "Not Very Satisfied", "Fairly Satisfied", "Very Satisfied"))

Gaps_probs$HOG <- factor(Gaps_probs$HOG , levels = c("Male Executive", "Female Executive"))


diff_hog_one <- subset(Gaps_probs, Satisfaction == "Not At All Satisfied")
diff_hog_two <- subset(Gaps_probs, Satisfaction == "Not Very Satisfied")
diff_hog_three <- subset(Gaps_probs, Satisfaction == "Fairly Satisfied")
diff_hog_four <- subset(Gaps_probs, Satisfaction == "Very Satisfied")




#graph this-save as 6.5x6 inches

Hog_diff_1 <- ggplot(diff_hog_one, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)


Hog_diff_2 <- ggplot(diff_hog_two, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)



Hog_diff_3 <- ggplot(diff_hog_three, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)

Hog_diff_4 <- ggplot(diff_hog_four, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)






#' 
#' 
#' 
#' 
#' 
#' ### Put HOG into one figure
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------

ggarrange( p9, p10, p11, p12,
           Hog_diff_1, Hog_diff_2, Hog_diff_3, Hog_diff_4,
           ncol=4, nrow=2, common.legend = TRUE) #Save as 6.5x6.0 inches

#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp2_hog_exec.pdf", width = 8.5, height = 5, units = "in")



#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' ## Government probabilities
#' 
#' 
#' 
#' 
#' ### Predictions 
#' 
## ---- echo= TRUE, message = FALSE----------------------------------------------------------------------------------
rm(list=ls())

library(readxl)
library(ggplot2)
library(dplyr)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 1 executive graph 
########
gov_preds <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp1_government_ES.xlsx")

gov_preds$Government[gov_preds$Government ==  0 ] <- "Loser"
gov_preds$Government[gov_preds$Government ==  1 ] <- "Winner"

gov_preds$Gender[gov_preds$Gender ==  0] <- "Male"
gov_preds$Gender[gov_preds$Gender ==  1] <- "Female"

gov_preds$Satisfaction[gov_preds$Satisfaction ==  1] <- "Not At All Satisfied"
gov_preds$Satisfaction[gov_preds$Satisfaction ==  2] <- "Not Very Satisfied"
gov_preds$Satisfaction[gov_preds$Satisfaction ==  3] <- "Fairly Satisfied"
gov_preds$Satisfaction[gov_preds$Satisfaction ==  4] <- "Very Satisfied"

#Reorder the factor levels


gov_preds$Satisfaction <- factor(gov_preds$Satisfaction , levels = c("Not At All Satisfied", "Not Very Satisfied", "Fairly Satisfied", "Very Satisfied"))
gov_preds$Gender <- factor(gov_preds$Gender , levels = c("Male","Female"))
gov_preds$Government <- factor(gov_preds$Government , levels = c("Loser","Winner"))


gov_preds$Groups <- paste(gov_preds$Gender, gov_preds$Government)



plot1 <- ggplot(gov_preds, aes(x = Satisfaction, y = Probabilities, colour = Groups)) +
  labs(x="Satisfaction Category", y="Pr(Fall in Given Category)") +
  ylim(0, .60) +
  theme_bw(16) +  
  theme(legend.position = c(.82, 0.25),  legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank()) +
  geom_point(aes(shape=Groups, color=Groups),size = 3, position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + coord_flip()






#' 
#' 
#' 
#' ### Differences-Hyp 1
## ------------------------------------------------------------------------------------------------------------------

#rm(list=ls())

library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 1 differences graph 
########
Gaps_probs <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp1_diff_gov.xlsx")


Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  1] <- "Not At All Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  2] <- "Not Very Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  3] <- "Fairly Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  4] <- "Very Satisfied"



Gaps_probs$Satisfaction <- factor(Gaps_probs$Satisfaction , levels = c("Not At All Satisfied", "Not Very Satisfied", "Fairly Satisfied", "Very Satisfied"))



#graph this-save as 6.5x6 inches

plot2 <- ggplot(Gaps_probs, aes(x = Satisfaction, y = Gaps)) +
  labs(x="Satisfaction Category", y="Satisfaction Differences (Boost for Women - Boost for Men)") +  
  ylim(-.03, .03) +
  theme_bw(16) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                         panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + coord_flip() 








#' 
#' 
## ------------------------------------------------------------------------------------------------------------------

ggarrange(plot1, plot2,
          ncol=1, nrow=2, common.legend = TRUE) #Save as 6.5x6.0 inches

#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp1_gov_fig.pdf", width = 8.5, height = 5.5, units = "in")



#' 
#' 
#' ### Legislative-Gov
#' 
#' 
#' 
#' #### Predictions
#' 
#' 
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------
rm(list=ls())

library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 2 government graph 
########
hyp2_descrip_gov <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_legis_gov.xlsx")

hyp2_descrip_gov$Government[hyp2_descrip_gov$Government ==  0 ] <- "Loser"
hyp2_descrip_gov$Government[hyp2_descrip_gov$Government ==  1 ] <- "Winner"

hyp2_descrip_gov$Gender[hyp2_descrip_gov$Gender ==  0] <- "Male"
hyp2_descrip_gov$Gender[hyp2_descrip_gov$Gender ==  1] <- "Female"


hyp2_descrip_gov$Gender <- factor(hyp2_descrip_gov$Gender , levels = c("Male","Female"))

hyp2_descrip_gov$Satisfaction[hyp2_descrip_gov$Satisfaction ==  1] <- "Not At All Satisfied"
hyp2_descrip_gov$Satisfaction[hyp2_descrip_gov$Satisfaction ==  2] <- "Not Very Satisfied"
hyp2_descrip_gov$Satisfaction[hyp2_descrip_gov$Satisfaction ==  3] <- "Fairly Satisfied"
hyp2_descrip_gov$Satisfaction[hyp2_descrip_gov$Satisfaction ==  4] <- "Very Satisfied"




hyp2_descrip_gov$Groups <- paste(hyp2_descrip_gov$Gender, hyp2_descrip_gov$Government)

satisfaction_legis_one <- subset(hyp2_descrip_gov, Satisfaction == "Not At All Satisfied")
satisfaction_legis_two <- subset(hyp2_descrip_gov, Satisfaction == "Not Very Satisfied")
satisfaction_legis_three <- subset(hyp2_descrip_gov, Satisfaction == "Fairly Satisfied")
satisfaction_legis_four <- subset(hyp2_descrip_gov, Satisfaction ==  "Very Satisfied")


#Not at all satisfied
p9 <- ggplot(satisfaction_legis_one, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Not At All Satisfied)") +  
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)



#Not very satisfied 
p10 <- ggplot(satisfaction_legis_two, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Not Very Satisfied)") +  
  ylim(.14, .42) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)





#Fairly satisfied 
p11 <- ggplot(satisfaction_legis_three, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Fairly Satisfied)") +  
  ylim(.32, .6) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)



#Very satisfied 
p12 <- ggplot(satisfaction_legis_four, aes(x = Descriptive, y = Probabilities, colour = Groups)) +
  labs(x="Percentage Female Legislators", y="Pr(Very Satisfied)") +  
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position = "bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(aes(color=Groups),position=position_dodge(width=0)) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper,color=Groups),linetype=2, alpha=0.0) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) + facet_wrap(. ~ Satisfaction)






#' 
#' 
#' ####  Differences
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------


library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


########
###Hypothesis 2 government graph 
########


Gaps_probs <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_legis_diff_gov.xlsx")

Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  1] <- "Not At All Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  2] <- "Not Very Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  3] <- "Fairly Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  4] <- "Very Satisfied"



Gaps_probs$Gaps_gender <- as.numeric(Gaps_probs$Gaps_gender)
Gaps_probs$Descriptive <- as.numeric(Gaps_probs$Descriptive)
Gaps_probs$Satisfaction <- as.factor(Gaps_probs$Satisfaction)


diff_legis_one <- subset(Gaps_probs, Satisfaction == "Not At All Satisfied")
diff_legis_two <- subset(Gaps_probs, Satisfaction == "Not Very Satisfied")
diff_legis_three <- subset(Gaps_probs, Satisfaction == "Fairly Satisfied")
diff_legis_four <- subset(Gaps_probs, Satisfaction == "Very Satisfied")








Legis_hog_diff_1 <- ggplot(diff_legis_one, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)





Legis_hog_diff_2  <- ggplot(diff_legis_two, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)




Legis_hog_diff_3  <- ggplot(diff_legis_three, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)




Legis_hog_diff_4 <- ggplot(diff_legis_four, aes(x = Descriptive, y = Gaps_gender)) +
  labs(x="Percentage Female Legislators", y="Boost for Women - Boost for Men") +  
  ylim(-.03, .03) +
  theme_bw(6) +   theme(legend.position="none", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_line(position=position_dodge(width=0)) + 
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_ribbon(aes(ymin=Lower,ymax=Upper),linetype=2, alpha=0.3) +  facet_wrap(. ~ Satisfaction)



#' 
#' 
#' ####  Put them together
#' 
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------


ggarrange( p9, p10, p11, p12,
           Legis_hog_diff_1, Legis_hog_diff_2, Legis_hog_diff_3, Legis_hog_diff_4,
           ncol=4, nrow=2, common.legend = TRUE) #Save as 6.5x6.0 inches
f
#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp2_legis_gov.pdf", width = 8.5, height = 5, units = "in")



#' 
#' 
#' 
#' ### HOG-Gov
#' 
#' 
#' 
#' #### Predictions
#' 
#' 
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------
rm(list=ls())

library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 2 government graph 
########
hyp2_hog_gov <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_hog_gov.xlsx")

hyp2_hog_gov$Government[hyp2_hog_gov$Government ==  0 ] <- "Loser"
hyp2_hog_gov$Government[hyp2_hog_gov$Government ==  1 ] <- "Winner"

hyp2_hog_gov$Gender[hyp2_hog_gov$Gender ==  0] <- "Male"
hyp2_hog_gov$Gender[hyp2_hog_gov$Gender ==  1] <- "Female"

hyp2_hog_gov$HOG[hyp2_hog_gov$HOG ==  0] <- "Male Executive"
hyp2_hog_gov$HOG[hyp2_hog_gov$HOG ==  1] <- "Female Executive"



hyp2_hog_gov$HOG <- as.factor(hyp2_hog_gov$HOG)
hyp2_hog_gov$HOG <- factor(hyp2_hog_gov$HOG , levels = c("Male Executive","Female Executive"))

hyp2_hog_gov$Gender <- factor(hyp2_hog_gov$Gender , levels = c("Male","Female"))


hyp2_hog_gov$Groups <- paste(hyp2_hog_gov$Gender, hyp2_hog_gov$Government)


hyp2_hog_gov$Satisfaction[hyp2_hog_gov$Satisfaction ==  1] <- "Not At All Satisfied"
hyp2_hog_gov$Satisfaction[hyp2_hog_gov$Satisfaction ==  2] <- "Not Very Satisfied"
hyp2_hog_gov$Satisfaction[hyp2_hog_gov$Satisfaction ==  3] <- "Fairly Satisfied"
hyp2_hog_gov$Satisfaction[hyp2_hog_gov$Satisfaction ==  4] <- "Very Satisfied"


satisfaction_hog_one <- subset(hyp2_hog_gov, Satisfaction == "Not At All Satisfied")
satisfaction_hog_two <- subset(hyp2_hog_gov, Satisfaction == "Not Very Satisfied")
satisfaction_hog_three <- subset(hyp2_hog_gov, Satisfaction == "Fairly Satisfied")
satisfaction_hog_four <- subset(hyp2_hog_gov, Satisfaction == "Very Satisfied")




#Not at all satisfied
p9 <- ggplot(satisfaction_hog_one, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Not At All Satisfied)") +
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +  facet_wrap(. ~ Satisfaction)





#Not very satisfied 
p10 <- ggplot(satisfaction_hog_two, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Not Very Satisfied)") +  
  ylim(.14, .4) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) +  facet_wrap(. ~ Satisfaction)







#Fairly satisfied 
p11 <- ggplot(satisfaction_hog_three, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Fairly Satisfied)") +  
  ylim(.34, .6) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) +  facet_wrap(. ~ Satisfaction)





#Very satisfied 
p12 <- ggplot(satisfaction_hog_four, aes(x = HOG, y = Probabilities, colour = Groups)) +
  labs(x="Gender of Executive", y="Pr(Very Satisfied)") +  
  ylim(0, .27) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_point(aes(shape=Groups, color=Groups),position=position_dodge(width=0.2)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,color=Groups,width=0.0), position=position_dodge(width=0.2)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF")) +  facet_wrap(. ~ Satisfaction)







#' 
#' 
#' #### Differences
#' 
## ------------------------------------------------------------------------------------------------------------------



library(readxl)
library(ggplot2)
library(ggpubr) #to put them in one graph


#########
###Hypothesis 2 government graph 
########
Gaps_probs <- read_excel("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/Prediction Output/hyp2_hog_diff_gov.xlsx")


Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  1] <- "Not At All Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  2] <- "Not Very Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  3] <- "Fairly Satisfied"
Gaps_probs$Satisfaction[Gaps_probs$Satisfaction ==  4] <- "Very Satisfied"


Gaps_probs$HOG[Gaps_probs$HOG ==  0] <- "Male Executive"
Gaps_probs$HOG[Gaps_probs$HOG ==  1] <- "Female Executive"





Gaps_probs$Satisfaction <- factor(Gaps_probs$Satisfaction , levels = c("Not At All Satisfied", "Not Very Satisfied", "Fairly Satisfied", "Very Satisfied"))

Gaps_probs$HOG <- factor(Gaps_probs$HOG , levels = c("Male Executive", "Female Executive"))


diff_hog_one <- subset(Gaps_probs, Satisfaction == "Not At All Satisfied")
diff_hog_two <- subset(Gaps_probs, Satisfaction == "Not Very Satisfied")
diff_hog_three <- subset(Gaps_probs, Satisfaction == "Fairly Satisfied")
diff_hog_four <- subset(Gaps_probs, Satisfaction == "Very Satisfied")




#graph this-save as 6.5x6 inches

Hog_diff_1 <- ggplot(diff_hog_one, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.037, .041) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)


Hog_diff_2 <- ggplot(diff_hog_two, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.037, .041) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)



Hog_diff_3 <- ggplot(diff_hog_three, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.037, .041) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)

Hog_diff_4 <- ggplot(diff_hog_four, aes(x = HOG, y = Gap_hog)) +
  labs(x="Gender of Executive", y="Boost for Women - Boost for Men") +  
  ylim(-.037, .041) +
  theme_bw(6) +   theme(legend.position="bottom", legend.title = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), 
                        panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  +
  geom_hline(yintercept = 0, color = "grey", linetype = 2) + 
  geom_point(aes(),size = 3, position=position_dodge(width=0.15)) + 
  geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.0), position=position_dodge(width=0.15)) + 
  scale_color_manual(values=c("#660000", "#CC0000", "#0033FF", "#33CCFF"))  +
  facet_wrap(. ~ Satisfaction)






#' 
#' 
#' 
#' #### Put HOG gov together
#' 
#' 
## ------------------------------------------------------------------------------------------------------------------

ggarrange( p9, p10, p11, p12,
           Hog_diff_1, Hog_diff_2, Hog_diff_3, Hog_diff_4,
           ncol=4, nrow=2, common.legend = TRUE) #Save as 6.5x6.0 inches

#ggsave("~/Dropbox/Satisfaction-Gender/Data Analysis/Figures and Graphs/hyp2_hog_gov.pdf", width = 8.5, height = 5, units = "in")


#' ## Dotcharts
## ---- echo = TRUE, message = FALSE---------------------------------------------------------------------------------
rm(list=ls())

library(haven)
library(ggplot2)
library(ggpubr)
library(dplyr)


Winners_Losers <- read_dta("~/Dropbox/Satisfaction-Gender/Data Analysis/Analysis Data/Winners_Losers_Analysis_ES.dta")
Winners_Losers$Country[Winners_Losers$Country ==  "United States of America" ] <- "USA"


satisfaction_gender_agg <- aggregate(Satisfaction ~ Country_year + Female, Winners_Losers, mean)




satisfaction_gender_agg <- satisfaction_gender_agg[order(satisfaction_gender_agg$Country_year),]

satisfaction_gender_agg$Gender <- satisfaction_gender_agg$Female


satisfaction_gender_agg$Gender <- satisfaction_gender_agg$Female
satisfaction_gender_agg$Gender[satisfaction_gender_agg$Gender==  0 ] <- "Male"
satisfaction_gender_agg$Gender[satisfaction_gender_agg$Gender==  1 ] <- "Female"
satisfaction_gender_agg$Gender <- as.factor(satisfaction_gender_agg$Gender)



satisfaction_first <- satisfaction_gender_agg[1:152,]
satisfaction_second <- satisfaction_gender_agg[153:294,]


theme_dotplot <- theme_bw(12) +
  theme(axis.text.y = element_text(size = rel(.7)),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(size = rel(.9)),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.4),
        panel.grid.minor.x = element_blank(), 
        legend.position = "none")




satisfaction_first <- as.data.frame(satisfaction_first[order(rev(satisfaction_first$Country_year), decreasing = TRUE), ])

plot1 <- ggplot(satisfaction_first, aes(x = Satisfaction, y = reorder(Country_year, desc(Country_year)))) +
  geom_point(aes(shape=Gender, colour = Gender), size = 1.3, position=position_dodge(width=0)) + 
  scale_x_continuous(limits = c(1.4, 3.5)) +
  theme_bw(12) +
  theme(axis.text.y = element_text(size = rel(.7)),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(size = rel(.9)),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.4),
        panel.grid.minor.x = element_blank(), 
        legend.position = "bottom") +
  scale_color_manual(values=c("#CC0000", "#33CCFF")) + 
  xlab("Satisfaction") +
  ylab("")


satisfaction_second <- as.data.frame(satisfaction_second[order(rev(satisfaction_second$Country_year), decreasing = TRUE), ])

plot2 <- ggplot(satisfaction_second, aes(x = Satisfaction, y = reorder(Country_year, desc(Country_year)))) +
  geom_point(aes(shape=Gender, colour = Gender), size = 1.3, position=position_dodge(width=0)) + 
  scale_x_continuous(limits = c(1.4, 3.5)) +
  theme_bw(12) +
  theme(axis.text.y = element_text(size = rel(.65)),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(size = rel(.9)),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.4),
        panel.grid.minor.x = element_blank(), 
        legend.position = "none") +
  scale_color_manual(values=c("#CC0000", "#33CCFF")) + 
  xlab("Satisfaction") +
  ylab("")



ggarrange(plot1, plot2, ncol=2, nrow=1, common.legend = TRUE, legend="bottom") #5.5x7 inches




#################
#Make the same plot for winners/losers 


satisfaction_winner_agg <- aggregate(Satisfaction ~ Country_year + votedEXEC, Winners_Losers, mean)




satisfaction_winner_agg <- satisfaction_winner_agg[order(satisfaction_winner_agg$Country_year),]

satisfaction_winner_agg$Executive <- satisfaction_winner_agg$votedEXEC
satisfaction_winner_agg$Executive[satisfaction_winner_agg$Executive==  0 ] <- "Loser"
satisfaction_winner_agg$Executive[satisfaction_winner_agg$Executive==  1 ] <- "Winner"

satisfaction_winner_agg$Executive <- as.factor(satisfaction_winner_agg$Executive)

satisfaction_executive_first <- satisfaction_winner_agg[1:152,]
satisfaction_executive_second <- satisfaction_winner_agg[153:294,]




theme_dotplot <- theme_bw(12) +
  theme(axis.text.y = element_text(size = rel(.7)),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(size = rel(.9)),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.4),
        panel.grid.minor.x = element_blank(), 
        legend.position = "none")




satisfaction_executive_first <- as.data.frame(satisfaction_executive_first[order(rev(satisfaction_executive_first$Country_year), decreasing = TRUE), ])

plot3 <- ggplot(satisfaction_executive_first, aes(x = Satisfaction, y = reorder(Country_year, desc(Country_year)))) +
  geom_point(aes(shape=Executive, colour = Executive), size = 1.3, position=position_dodge(width=0)) + 
  scale_x_continuous(limits = c(1.4, 3.5)) +
  theme_bw(12) +
  theme(axis.text.y = element_text(size = rel(.7)),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(size = rel(.9)),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.4),
        panel.grid.minor.x = element_blank(), 
        legend.position = "bottom") +
  scale_color_manual(values=c("#CC0000", "#33CCFF")) + 
  xlab("Satisfaction") +
  ylab("")


satisfaction_executive_second <- as.data.frame(satisfaction_executive_second[order(rev(satisfaction_executive_second$Country_year), decreasing = TRUE), ])

plot4 <- ggplot(satisfaction_executive_second, aes(x = Satisfaction, y = reorder(Country_year, desc(Country_year)))) +
  geom_point(aes(shape=Executive, colour = Executive), size = 1.3, position=position_dodge(width=0)) + 
  scale_x_continuous(limits = c(1.4, 3.5)) +
  theme_bw(12) +
  theme(axis.text.y = element_text(size = rel(.65)),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(size = rel(.9)),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(size = 0.4),
        panel.grid.minor.x = element_blank(), 
        legend.position = "none") +
  scale_color_manual(values=c("#CC0000", "#33CCFF")) + 
  xlab("Satisfaction") +
  ylab("")



ggarrange(plot3, plot4, ncol=2, nrow=1, common.legend = TRUE, legend="bottom") #5.5x7 inches





